<template>
  <div id="login">
    <div class="login-container">
      <div class="avatar-box">
        <img src="~assets/img/logo/logo.png" alt="">
      </div>
      <!-- 登录表单区域 -->
      <el-form ref="loginFromRef" label-width="70px" class="login-from" :model="loginForm" :rules="rules">
        <!-- 1.用户名 -->
        <el-form-item label="用户名" prop='username'>
          <el-input prefix-icon="iconfont icon-user"
          v-model="loginForm.username"></el-input>
        </el-form-item>
        <!-- 2.密码 -->
        <el-form-item label="密码" prop="password">
          <el-input prefix-icon="iconfont icon-lock" v-model="loginForm.password" type="password"></el-input>
        </el-form-item>
        <!-- 3.按钮 -->
         <el-form-item class="btn">
          <el-button type="primary" @click="login">提交</el-button>
          <el-button type="info" @click="loginRefresh">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  components: {

  },
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 11, message: '长度在 6 到 11 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    loginRefresh () {
      // console.log(this)
      // 调用一下from表单的重置
      this.$refs.loginFromRef.resetFields()
    },
    login () {
      this.$refs.loginFromRef.validate(async valid => {
        // 这里是判断一下我的input那里 name 和密码 有没有搞好
        // !这个类似resetFields  和 validate  可以在我的element那里可以找到 后面有个from methods
        if (!valid) return
        const { data: res } = await this.$http.post('login', this.loginForm)
        // console.log(res)
        if (res.meta.status !== 200) return this.$message.error('登录失败！')
        // 提示登录成功
        this.$message.success('登录成功')
        // !token 这个玩意每次都会传一个不同的值
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>s

<style scoped>
  #login {
    background-color: #2b4b6b;
    height: 100%;
    position: relative;
  }

  .login-container {
    width: 450px;
    height: 300px;
    background-color: white;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
  .avatar-box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0,0,10px,#ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background: white;
  }
  .avatar-box img{
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }

  .btn {
    display: flex;
    justify-content: flex-start;
  }

  .login-from {
    position: absolute;
    bottom: 0px;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
</style>
